﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace DoNet.Common.Utilities
{
    public partial class DataCacheManager
    {
        public delegate DataSet DataCacheDelegate();
        public delegate DataSet DataCacheDelegateByPath(string path);
        public delegate DataSet DataCacheDelegateByTwoPath(string path1, string path2);
        public delegate DataSet DataCacheDelegateByThreePath(string path1, string path2, string path3);

        public static DataSet GetDataSet(string path, string CacheKey, DataCacheDelegateByPath dataCacheDelegate)
        {
            DataSet dataSet = (DataSet)DataCache.GetCache(CacheKey);

            if (dataSet == null)
            {
                try
                {
                    dataSet = dataCacheDelegate(path);

                    DataCache.SetCache(CacheKey, dataSet);
                }
                catch (Exception ee)
                {
                    //Log...
                    //DSCQuene.Instance.Send("GetDataSet"+ee.ToString());
                    throw new Exception(ee.ToString());
                }
            }
            return dataSet;
        }

        public static DataSet GetDataSet(string path1, string path2, string CacheKey, DataCacheDelegateByTwoPath dataCacheDelegate)
        {
            DataSet dataSet = (DataSet)DataCache.GetCache(CacheKey);

            if (dataSet == null)
            {
                try
                {
                    dataSet = dataCacheDelegate(path1, path2);

                    DataCache.SetCache(CacheKey, dataSet);
                }
                catch (Exception ee)
                {
                    //Log...
                    //DSCQuene.Instance.Send("GetDataSet"+ee.ToString());
                    throw new Exception(ee.ToString());
                }
            }
            return dataSet;
        }

        public static DataSet GetDataSet(string path1, string path2, string path3, string CacheKey, DataCacheDelegateByThreePath dataCacheDelegate)
        {
            DataSet dataSet = (DataSet)DataCache.GetCache(CacheKey);

            if (dataSet == null)
            {
                try
                {
                    dataSet = dataCacheDelegate(path1, path2, path3);

                    DataCache.SetCache(CacheKey, dataSet);
                }
                catch (Exception ee)
                {
                    //Log...
                    //DSCQuene.Instance.Send("GetDataSet"+ee.ToString());
                    throw new Exception(ee.ToString());
                }
            }
            return dataSet;
        }

        public static DataSet GetDataSet(string CacheKey, DataCacheDelegate dataCacheDelegate)
        {
            DataSet dataSet = (DataSet)DataCache.GetCache(CacheKey);
            if (dataSet == null)
            {
                try
                {
                    dataSet = dataCacheDelegate();

                    DataCache.SetCache(CacheKey, dataSet);
                }
                catch (Exception ee)
                {
                    //Log...
                    throw new Exception(ee.ToString());
                }
            }
            return dataSet;
        }

        public static void Remove(string CacheKey)
        {
            DataCache.RemoveCache(CacheKey);
        }
    }

    public partial class DataCacheManager
    {
        public static void SetCache(string cacheKey, object obj)
        {
            DataCache.SetCache(cacheKey, obj);

        }

        public static object GetCache(string cacheKey)
        {
            return DataCache.GetCache(cacheKey);

        }
    }

}
